/*
图论基础
啥都想往里塞（
*/

#include <bits/stdc++.h>

#define N 1005 //节点数

using namespace std;

int G[N][N] = {0};    //邻接矩阵，从前到后
//假定边权均>0,则G[x][y] == 0 表示x，y间没有连边

struct edge{//邻接表，从u到v，权为val;
    //int u;
    int v;
    int val;
};

vector <edge> g[N]; //N

//Tarjan算法部分
int dfn[N]; //dfs时间戳
int vis[N] = {0};
int low[N]; //最早能回溯到的时间
stack <int> s;
int scc[N];
int scc_cnt = 0;
int tim = 0;

void tarjan(int u){ //求解强连通分量——Tarjan
    dfn[u] = low[u] = ++tim;
    s.push(u);  vis[u] = 1;
    for(edge v: g[u]){
        if(!dfn[v.v]){      //没访问过
            tarjan(v.v);
            low[u] = min(low[u], low[v.v]);
        }else if(vis[v.v]){ //发现回边，v.v是u的祖先节点（dfs序下）
            low[u] = min(low[u], dfn[v.v]);
        }
    }
    //如果low[u] == dfn[u]，则说明找到了一个强连通分量
    if(low[u] == dfn[u]){
        scc_cnt++;
        while(!s.empty() && s.top() != u){
            int v = s.top();
            s.pop();
            scc[v] = scc_cnt;
            vis[v] = false;
        }
        s.pop();
        scc[u] = scc_cnt;
        vis[u] = false;
    }
    return;
}

int main(){
    int n, m;   //n个点，m条边
    cin >> n >> m;
    int u, v, val;
    for(int i = 1; i <= m; i++){
        scanf("%d %d %d", &u, &v, &val);
        g[u].push_back((edge){v, val}); //邻接表存边
        //g[v].push_back((edge){u, val}); //无向图须存反向边
        G[u][v] = val;  //邻接矩阵存边
        //G[v][u] = val;
    }
    memset(vis, 0, sizeof(vis));
    memset(dfn, 0, sizeof(dfn));
    for(int i = 1; i <= n; i++){
        if(!dfn[i]) tarjan(i);
    }
    for(int i = 1; i <= n; i++){
        cout << "Node " << i << " belongs to SCC " << scc[i] << endl;
    }
    return 0;
}